<template>
  <div :id="chartId" style="width: 100%; height: 350px;position:relative"></div>
</template>

<script>
import service from "@/service/projects";

export default {
  props: [
    "chartId",
  ],
  data() {
    return {
      projectTypes: ['规划基金项目', '青年基金项目']
    };
  },
  computed: {},
  watch: {
    type: function (newVal, oldVal) {
      newVal && this.loadData();
    },
  },
  mounted: function () {
    this.loadData();
  },
  methods: {
    loadData() {
      this.chartDiv = echarts.init(document.getElementById(this.chartId));
      this.chartDiv.showLoading({
          text: '数据正在加载...',
          textStyle: { fontSize : 30 , color: '#444' },
          effectOption: {backgroundColor: 'rgba(0, 0, 0, 0)'}
      });
      service.getDatas({
          project_type: this.projectTypes   ,
          columns: ["project_type", "approval_year", "count:id"],
          group: ["project_type", "approval_year"],
          order: [
            ["approval_year", 1],
            ["count:id", 1],
          ],
          pageSize: 0,
        }, '1058').then((res) => {
          this.initData(res.data);
        this.chartDiv.hideLoading();
        });
    },
    initData(data) {
      const xDataSet = new Set();
      data.forEach((item) => {
        xDataSet.add(item.approval_year);
      });
      const xDataArr = Array.from(xDataSet).sort((a, b) => a - b);
      const dataArrGH = Array.apply(null, { length: xDataArr.length });
      const dataArrQY = Array.apply(null, { length: xDataArr.length });
      xDataArr.forEach((xItem, xIndex) => {
        data.forEach((item) => {
          if (xItem === item.approval_year) {
            if (item.project_type == this.projectTypes[0]) {
              dataArrGH[xIndex] = item._count_id;
            } else if (item.project_type == this.projectTypes[1]) {
              dataArrQY[xIndex] = item._count_id;
            }
          }
        });
      });

      this.initChart(xDataArr, dataArrGH, dataArrQY);
    },
    initChart(xDataArr, dataArrGH, dataArrQY) {
      if (!document.getElementById(this.chartId)) return;
      this.chartDiv = echarts.init(document.getElementById(this.chartId));
      const option = {
        color: ["#1bc481", "#ffaf3c", "#eb5e66", "#3898e5", "#7EC0EE", "#FF9F7F", "#FFD700", "#C9C9C9", "#E066FF", "#C0FF3E"],
        animation: false,
        grid: {
          top: "50",
          left: "32",
          bottom: "30",
          right: "39",
          containLabel: true,
        },
        legend: {
          show: true,
          bottom: 0,
          data: this.projectTypes,
          textStyle: {
          },
        },
        tooltip: {
          trigger: "axis",
        },
        xAxis: [
          {
            type: "category",
            nameTextStyle: {
            fontSize: 12, 
            fontFamily: "微软雅黑",
            align: "center",
         },
            data: xDataArr,
            axisTick: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "rgba(202,202,202,0.15)",
              },
            },
            axisLabel: {
              textStyle: {
                color: "black",
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            // name: '项目数量',
            nameTextStyle: {
              fontSize: 12, // 这个会影响坐标轴的字体大小，不懂为什么
              fontFamily: "微软雅黑",
            },
            axisLabel: {
              textStyle: {
                // color: 'white',
                color: "black",
              },
              formatter: "{value}",
            },
            splitLine: {
              show: true,
              lineStyle: {
                // type: "dashed",
                color: "#E9E9E9",
              },
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: "RGB(47,68,114)",
              },
            },
          },
        ],
        series: [
          {
            name: this.projectTypes[0],
            type: "bar",
            data: dataArrQY,
          },
          {
            name: this.projectTypes[1],
            type: "bar",
            data: dataArrGH,
          },
        ],
      };

      this.chartDiv.setOption(option);

      window.onresize = ()=>{
        this.chartDiv.resize();
      }
    },
  },
};
</script>

<style scoped>
</style>
